﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp0
{
    public partial class shang_dan_2songUserControl1 : UserControl
    {
        public shang_dan_2songUserControl1()
        {
            InitializeComponent();
        }

        private void dataGridView2_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }
        private void Table_song()  //待送货
        {
            dataGridView2.Rows.Clear();
            string sql1 = $"select o.Oid,m.Mid,m.Mname,m.Munit,m.Mnorm,o.Onum,c.Cname,o.Oplace,o.Otime,o.Ostate from OrderM o,Customer c, Merchandise m  where o.Mid = m.Mid and o.Cid = c.Cid and o.Ostate in ('待送货', '送货中')";
            Dao dao = new Dao();
            IDataReader dr1 = dao.read(sql1);
            string a, b, c,  d, e, f,  g, h, k,p;
            while (dr1.Read())
            {

                a = dr1["Oid"].ToString();
                b = dr1["Mid"].ToString();
                c = dr1["Mname"].ToString();
                d = dr1["Munit"].ToString();
                e = dr1["Mnorm"].ToString();
                f = dr1["Onum"].ToString();
                g = dr1["Cname"].ToString();
                h = dr1["Oplace"].ToString();  //地址
                k = dr1["Otime"].ToString(); //时间
                p = dr1["Ostate"].ToString();
                string[] str = { a, b, c, d, e, f, g, h, k,p };
                dataGridView2.Rows.Add(str);
            }
            dr1.Close();//关闭连接 
        }
        private void shang_dan_2songUserControl1_Load(object sender, EventArgs e)
        {
            Table_song();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            //送货完成
          
            string Oid = dataGridView2.SelectedRows[0].Cells[0].Value.ToString();
            string sql1 = $"update OrderM set Ostate='已完成' where Oid='{Oid}'";
            Dao dao = new Dao();
            int n = dao.Execute(sql1);

            if (n > 0)
            {//售出时间
                MessageBox.Show(sql1);
            }
            Table_song();


        }

        private void button2_Click(object sender, EventArgs e)
        {//显示所有订单
            Table_song();
        }

        private void button5_Click(object sender, EventArgs e)
        {//按客户名称查询，text 3
            Cname_search();
        }
        private void Cname_search()
        {
            dataGridView2.Rows.Clear();
            string sql1 = $"select o.Oid,m.Mid,m.Mname,m.Munit,m.Mnorm,o.Onum,c.Cname,o.Oplace,o.Otime,o.Ostate from OrderM o,Customer c, Merchandise m where o.Mid = m.Mid and o.Cid = c.Cid and o.Ostate = '待送货'and c.Cname like '%{textBox3.Text}%'";
            Dao dao = new Dao();
            IDataReader dr1 = dao.read(sql1);
            string a, b, c , d , e , f , g , h ,k,p;
            while (dr1.Read())
            {
                
                a = dr1["Oid"].ToString();
                b = dr1["Mid"].ToString();
                c = dr1["Mname"].ToString();

                d = dr1["Munit"].ToString();
                e = dr1["Mnorm"].ToString();

                f = dr1["Onum"].ToString();
                g = dr1["Cname"].ToString();

                h = dr1["Oplace"].ToString();  //地址
                k = dr1["Otime"].ToString(); //时间
                p = dr1["Ostate"].ToString();
                string[] str = { a, b, c, d, e, f, g, h,k,p };
                dataGridView2.Rows.Add(str);
            }
            dr1.Close();//关闭连接 

        }

        private void textBox3_TextChanged(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            //选择订单，查询 商品 的存放信息,显示在dataGridView1
            
            string mid = dataGridView2.SelectedRows[0].Cells[1].Value.ToString();//获取编号
            dataGridView1.Rows.Clear();
            string sql1 = $"select m.Mid,m.Mname,d.Iplace,d.Dstock from Merchandise m join Depot d on m.Mid = d.Mid where m.Mid='{mid}'";
            Dao dao = new Dao();
            IDataReader dr1 = dao.read(sql1);
            string a, b, c, d;
            while (dr1.Read())
            {
                a = dr1["Mid"].ToString();
                b = dr1["Mname"].ToString();

                c = dr1["Iplace"].ToString();
                d = dr1["Dstock"].ToString();
                
                string[] str = { a, b, c, d};
                dataGridView1.Rows.Add(str);
            }
            dr1.Close();

        }

        private void panel1_Paint(object sender, PaintEventArgs e)
        {

        }

        private void button3_Click(object sender, EventArgs e)
        {//取货，text 2
            
            string id = dataGridView1.SelectedRows[0].Cells[0].Value.ToString();//获取编号
            string place= dataGridView1.SelectedRows[0].Cells[2].Value.ToString();


            dataGridView1.Rows.Clear();
            string sql1 = $"update Depot set Dstock=Dstock-{textBox2.Text} where mid='{id}'and Iplace='{place}'and (Dstock-{textBox2.Text})>=0 ";
            Dao dao = new Dao();
            
            int n = dao.Execute(sql1);
            if (n > 0)
            {
                MessageBox.Show("成功");
            }
            else
            {
                MessageBox.Show("失败");
            }
            string sql2 = $"select m.Mid,m.Mname,d.Iplace,d.Dstock from Merchandise m join Depot d on m.Mid = d.Mid where m.Mid='{id}'";

            IDataReader dr1 = dao.read(sql2);
            string a, b, c, d;
            while (dr1.Read())
            {


                a = dr1["Mid"].ToString();
                b = dr1["Mname"].ToString();

                c = dr1["Iplace"].ToString();
                d = dr1["Dstock"].ToString();

                string[] str = { a, b, c, d };
                dataGridView1.Rows.Add(str);
            }
            dr1.Close();

        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {

        }

        private void button6_Click(object sender, EventArgs e)
        {//状态 待送货-》送货中
            string Oid = dataGridView2.SelectedRows[0].Cells[0].Value.ToString();
            string sql1 = $"update OrderM set Ostate='送货中' where Oid='{Oid}'";
            Dao dao = new Dao();
            int n = dao.Execute(sql1);

            if (n > 0)
            {
               // MessageBox.Show(sql1);
            }
            Table_song();
        }
    }
}
